﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

namespace KiDS_Projekat
{

	public class ExternalNode
	{
		public string Location { get; set; }
		public double Temperature { get; set; }
	}
	/// <summary>
	/// Predstavlja cvor u arhitekturi distribuiranog sistema
	/// </summary>
	public class Node
	{
		private readonly List<string> _levels = new List<string>();
		private readonly List<bool> _master = new List<bool>();
		private readonly Dictionary<string, List<Slave>> _slaves = new Dictionary<string, List<Slave>>();
		private readonly ConcurrentDictionary<string, List<Master>> _myMasters = new ConcurrentDictionary<string, List<Master>>();
		private readonly List<ExternalNode> _collectedTemperatures = new List<ExternalNode>();

		private ushort _port;
		private string _address;
		private string _location;
		
		public Node(string location, ushort port)
		{
			_location = location;
			_port = port;

			string[] loc = location.Split(',');
			foreach (var l in loc)
				addLevel(l);
			
		}


		public void SetMaster(string level, bool isMaster)
		{
			for (int i = 0; i < _levels.Count; i++)
				if (_levels[i] == level) { 
					_master[i] = isMaster;
					break;
				}
		}

		private void addLevel(string level)
		{
			_levels.Add(level);
			_master.Add(false);
		}

		public void AddSlave(string level, Slave slave)
		{
			List<Slave> list;
			if (!_slaves.TryGetValue(level, out list))
			{
				list = new List<Slave>();
				_slaves[level] = list;
			}
			list.Add(slave);
		}

		public void AddMaster(string level, Master master)
		{
			List<Master> list;
			if (!_myMasters.TryGetValue(level, out list))
			{
				list = new List<Master>();
				_myMasters[level] = list;
			}
			list.Add(master);
		}

		public ushort Port
		{
			get
			{
				return _port;
			}
		}

		public string Location
		{
			get
			{
				return _location;
			}
		}

		public string Address
		{
			get
			{
				return _address;
			}
			set
			{
				
				_address = value;
				if (_address == "127.0.0.1" || _address == "0.0.0.0")
					_address = Properties.Settings.Default.DefaultLokacija;
			}
		}

		
		public ConcurrentDictionary<string, List<Master>> Masters
		{
			get
			{
				return _myMasters;
			}
		}

		public List<ExternalNode> CollectedTemperatures
		{
			get
			{
				return _collectedTemperatures;
			}
		}

		internal void CollectTemperature(string location, double temperature)
		{
			for (int i = 0; i < _collectedTemperatures.Count; i++)
			{
				if (_collectedTemperatures[i].Location == location)
				{
					_collectedTemperatures[i].Temperature = temperature;
					return;
				}
			}

			_collectedTemperatures.Add(new ExternalNode()
			{
				Location = location,
				Temperature = temperature
			});
			Program.Terminal.Print("Collected temperatures count: " + _collectedTemperatures.Count);
		}

	}
}
